package other_transform;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import com.apiexample.server.dao.APISearchRecordDao;
import com.apiexample.server.dao.ClassDao;
import com.apiexample.server.dao.ExampleClassDao;
import com.apiexample.server.entity.APISearchRecordEntity;
import com.apiexample.server.entity.ClassEntity;
import com.apiexample.server.entity.ExampleClassRelation;

import dao.APIDao;
import dao.QueryDao;
import dao.entity.QueryEntity;

public class TransformTopSearchedAPI {
	public static void main(String[] args) {
//		Connection apiexample_conn = dao.BaseDao.getTestConn();
		Connection apiserver_conn = com.apiexample.server.dao.BaseDao
				.getTestConn();
		System.out.println("1 ");
//		ArrayList<QueryEntity> allQueriess = QueryDao
//				.getAllQuery(apiexample_conn);
		System.out.println("2 ");
//		HashMap<Integer, String> apiIdMapName = APIDao
//				.getIDMapName(apiexample_conn);
		System.out.println("3 ");
		
		ArrayList<ClassEntity> classList = ClassDao.getAllClassList(apiserver_conn);
		System.out.println("4 ");
		HashSet<String> classNameList = new HashSet<String>();
		HashMap<String, Integer> classNameIdMap = new HashMap<String, Integer>();
		for (ClassEntity classEntity : classList) {
			classNameList.add(classEntity.getName());
			classNameIdMap.put(classEntity.getName(), classEntity.getId());
		}
		System.out.println("5 ");
//		for (QueryEntity queryEntity : allQueriess) {
			for (ClassEntity classEntity : classList) {
//				String classname = apiIdMapName.get(queryEntity.getApi_id());
				String classname = classEntity.getName();
				if(classname == null){
					System.err.println("error");
					continue;
				}
//				if(classNameList.contains(classname)){
//					Integer classIdInAPIServer = classNameIdMap.get(classname);
					int classId = classEntity.getId();
					int examplecount = 0;
//					if(classIdInAPIServer!=null){
						ArrayList<Integer> exampleList = ExampleClassDao.getExampleIDListByClassID(apiserver_conn,classId, ExampleClassRelation.REL_TYPE_ALL);
						examplecount = exampleList.size();
//					}else{
//						continue;
//					}		
					
//					int googleresultcount = queryEntity.getResult_count();
					if(examplecount==0){
						continue;
					}
					APISearchRecordEntity entity = new APISearchRecordEntity(classname, 0, examplecount, 0);
					System.out.println("Insert "+classname+" "+0+" "+examplecount+" "+0);
					APISearchRecordDao.insert(apiserver_conn, entity);
//				}			
			}
			
//		}
	}
}
